{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## NumPy 矩阵乘法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在前面几个视频中，你已经听了很多关于矩阵乘法的知识 – 现在，你将看到如何使用 NumPy 进行矩阵乘法运算。但是，你要知道 NumPy 支持几种类型的矩阵乘法。\n",
    "\n",
    "### 元素级乘法\n",
    "你已看过了一些元素级乘法。你可以使用 multiply 函数或 * 运算符来实现。回顾一下，它看起来是这样的："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.25,  1.  ,  2.25],\n",
       "       [ 4.  ,  6.25,  9.  ]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = np.array([[1,2,3],[4,5,6]])\n",
    "m\n",
    "# 显示以下结果：\n",
    "# array([[1, 2, 3],\n",
    "#        [4, 5, 6]])\n",
    "\n",
    "n = m * 0.25\n",
    "n\n",
    "# 显示以下结果：\n",
    "# array([[ 0.25,  0.5 ,  0.75],\n",
    "#        [ 1.  ,  1.25,  1.5 ]])\n",
    "\n",
    "m * n\n",
    "# 显示以下结果：\n",
    "# array([[ 0.25,  1.  ,  2.25],\n",
    "#        [ 4.  ,  6.25,  9.  ]])\n",
    "\n",
    "np.multiply(m, n)   # 相当于 m * n\n",
    "# 显示以下结果：\n",
    "# array([[ 0.25,  1.  ,  2.25],\n",
    "#        [ 4.  ,  6.25,  9.  ]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 矩阵乘积\n",
    "要获得矩阵乘积，你可以使用 NumPy 的 matmul 函数。\n",
    "\n",
    "如果你有兼容的形状，那就像这样简单："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1,2,3,4],[5,6,7,8]])\n",
    "a\n",
    "# 显示以下结果：\n",
    "# array([[1, 2, 3, 4],\n",
    "#        [5, 6, 7, 8]])\n",
    "a.shape\n",
    "# 显示以下结果：\n",
    "# (2, 4)\n",
    "\n",
    "b = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])\n",
    "b\n",
    "# 显示以下结果：\n",
    "# array([[ 1,  2,  3],\n",
    "#        [ 4,  5,  6],\n",
    "#        [ 7,  8,  9],\n",
    "#        [10, 11, 12]])\n",
    "b.shape\n",
    "# 显示以下结果：\n",
    "# (4, 3)\n",
    "\n",
    "c = np.matmul(a, b)\n",
    "c\n",
    "# 显示以下结果：\n",
    "# array([[ 70,  80,  90],\n",
    "#        [158, 184, 210]])\n",
    "c.shape\n",
    "# 显示以下结果：\n",
    "# (2, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果您的矩阵具有不兼容的形状，则会出现以下错误："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "shapes (4,3) and (2,4) not aligned: 3 (dim 1) != 2 (dim 0)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-4-d215e6c9a34a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatmul\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0;31m# 显示以下错误：\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;31m# ValueError: shapes (4,3) and (2,4) not aligned: 3 (dim 1) != 2 (dim 0)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: shapes (4,3) and (2,4) not aligned: 3 (dim 1) != 2 (dim 0)"
     ]
    }
   ],
   "source": [
    "np.matmul(b, a)\n",
    "# 显示以下错误：\n",
    "# ValueError: shapes (4,3) and (2,4) not aligned: 3 (dim 1) != 2 (dim 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### NumPy 的 dot 函数\n",
    "有时候，在你以为用 matmul 函数的地方，可能会看到 NumPy 的 dot 函数。事实证明，如果矩阵是二维的，那么 dot 和 matmul 函数的结果是相同的*。\n",
    "\n",
    "所以这两个结果是等价的："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7, 10],\n",
       "       [15, 22]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1,2],[3,4]])\n",
    "a\n",
    "# 显示以下结果：\n",
    "# array([[1, 2],\n",
    "#        [3, 4]])\n",
    "\n",
    "np.dot(a,a)\n",
    "# 显示以下结果：\n",
    "# array([[ 7, 10],\n",
    "#        [15, 22]])\n",
    "\n",
    "a.dot(a)  # you can call你可以直接对 `ndarray` 调用 `dot` \n",
    "# 显示以下结果：\n",
    "# array([[ 7, 10],\n",
    "#        [15, 22]])\n",
    "\n",
    "np.matmul(a,a)\n",
    "# array([[ 7, 10],\n",
    "#        [15, 22]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "虽然这些函数对于二维数据返回相同的结果，但在用于其他数据形状时，你应该谨慎选择要使用哪种。你可以在 matmul 和 dot 文档中详细了解它们的差异，并找到其他 NumPy 函数的链接。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 关于点积的更多信息，可以访问[Dot product](https://en.wikipedia.org/wiki/Dot_product)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
